import JSONUtil from "../utils/JSONUtil";
import ResponseUtil from "./ResponseUtil";
import ActionParam from "../core/action/ActionParam";

class RequestParser {

    /**
     * 处理响应
     * @param {ActionParam} actionParam 控制器参数
     * @param {any} res 返回数据
     */
    resolve(actionParam: ActionParam, res: any) {
        let endRes
        if (!res) {
            endRes = 'request error';
        }
        if (typeof res === 'string') {
            endRes = res;
        } else if (JSONUtil.isJSON(res)) {
            ResponseUtil.setHeaderContentTypeJSON(actionParam.response)
            endRes = JSON.stringify(res);
        } else {
            endRes = res.toString();
        }
        actionParam.response.end(endRes)
    }
}

export default new RequestParser()
